package com.zxm.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.zxm.ssm.Aspect.Interfaces.Permission;
import com.zxm.ssm.entity.RegisterUser;
import com.zxm.ssm.entity.User;
import com.zxm.ssm.service.UserService;
import com.zxm.ssm.utils.SessionStaticMes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author ZXM
 * @ClassName UserController
 * @Description TODO
 * @date 2021/7/18 10:18
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("login")
    public String toLogin(User user) {
        return "user/login";
    }

    @PostMapping("login")
    public String doLogin(@Validated User user, BindingResult result, Model model, HttpSession session) {
        if (result.hasErrors()) {
            return "user/login";
        }
        User dbUser = userService.findUserByNameAndPwd(user);
        if (dbUser == null) {
            model.addAttribute("msg", "用户名或密码错误！");
            /*result.reject("用户名或密码错误！");*/
            return "user/login";
        } else {
            session.setAttribute(SessionStaticMes.USER_IN_SESSION, dbUser);
            // 验证通过
            if(dbUser.getRole() == 1){
                return "redirect: /ssm/fount/fount_product_list";
            }else{
                return "redirect: list";
            }
        }
    }

    @RequestMapping("register")
    public String doRegister(@RequestParam("name") String name, @RequestParam("newPassword") String newPassword, @RequestParam("repeatPassword") String repeatPassword) {
        RegisterUser registerUser = new RegisterUser();
        registerUser.setName(name);
        registerUser.setPassword(newPassword);
        registerUser.setRepeatPassword(repeatPassword);
        if (registerUser.getRepeatPassword().equals(registerUser.getPassword())) {
            userService.addUser(registerUser);
            return "redirect: /ssm/user/login";
        } else {
            return "user/login";
        }

    }


    @RequestMapping("exit")
    public String exit(HttpSession session) {
        session.removeAttribute("user");
        return "redirect: /ssm/user/login";
    }

    @Permission
    @RequestMapping("list")
    public String getUserList(Model model, @RequestParam(required = false, defaultValue = "1") int pageNum, @RequestParam(required = false, defaultValue = "4") int pageSize) {

        PageInfo<User> pageInfo = userService.findAllUsers(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return "user/list";
    }

    @Permission
    @GetMapping("add")
    public String toAdd(User user) {
        return "user/add";
    }

    @Permission
    @PostMapping("add")
    //找项目里面upload在哪里，写HttpServletRequest
    //@Validated检验
    public String doAdd(User user, HttpServletRequest request) throws IOException {

        Integer ret = userService.saveUser(user);
        if (ret > 0) {
            return "redirect:/user/list";
        }

        return "user/add";
    }

    @Permission
    @GetMapping("edit")
    public String toEdit(int id, User user, Model model) {
        user = userService.findUserById(id);
        model.addAttribute("user", user);
        return "user/edit";
    }
    @Permission
    @PostMapping("edit")
    //找项目里面upload在哪里，写HttpServletRequest
    //@Validated检验
    public String doEdit(@Validated User user, BindingResult result, Model model, HttpServletRequest request) throws IOException {
        if (result.hasErrors()) {
            return "user/edit";
        }
        Integer ret = userService.UpdateUser(user);
        if (ret > 0) {
            return "redirect:/user/list";
        }
        return "user/edit";
    }

    @Permission
    @RequestMapping("del")
    public String del(int id) {
        userService.delUserById(id);
        return "redirect:/user/list";
    }
}
